<?php

include "validate.php";

if (isset($_POST['name']) && strlen($_POST['name']) > 0)
{
	$name = $_POST['name'];
	if (isset($_POST['name']))
		$name = $_POST['name'];
	else
		$name = "";
	if (isset($_POST['region']))
		$region = $_POST['region'];
	else
		$region = "";
	if (isset($_POST['address']))
		$address = $_POST['address'];
	else
		$address = "";
	if (isset($_POST['comments']))
		$comments = $_POST['comments'];
	else
		$comments = "";
	
	$name = mysql_real_escape_string($name);
	$region = mysql_real_escape_string($region);
	$address = mysql_real_escape_string($address);
	$comments = mysql_real_escape_string($comments);
	
	$query = "	INSERT INTO sensor (name";
	$values = "	VALUES ('" . $name;
	
	if (strlen($region)>0)
	{
		$query = $query . ", region";
		$values = $values . "', '" . $region;
	}

	if (strlen($address)>0)
	{
		$query = $query . ", address";
		$values = $values . "', '" . $address;
	}

	if (strlen($comments)>0)
	{
		$query = $query . ", comments";
		$values = $values . "', '" . $comments;
	}

	$query = $query . ", counter, last_update) ";
	$values = $values . "', '0'" . ", " . date(0) . ");";

	$query = $query . $values;
	
	mysql_query($query);
	$_SESSION["message"] = "Sensor Created";
	header("Location:sensor.php");
}
else
{
	$_SESSION["message"] = "Please give the sensor a name.";
	header("Location:sensor.php");
}
?>
